import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-07-24
 * Time: 8:42
 */
public class Test {
    // 计数排序
    public static void countSort(int[] array){
        // write code  here
        int minVal = array[0];
        int maxVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if (minVal > array[i]) {
                minVal = array[i];
            }
            if (maxVal < array[i]) {
                maxVal = array[i];
            }
        }
        int[] count = new int[maxVal - minVal + 1];
        for (int i = 0; i < array.length; i++) {
            count[array[i] - minVal]++;
        }
        int k = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] != 0) {
                array[k++] = i + minVal;
                count[i]--;
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {2,7,2,12,1,5,10,9,3,0};
        countSort(array);
        System.out.println(Arrays.toString(array));
    }
}
